import {
  getGameListService,
  getGameDetailService,
  getGameListBySellerService,
  addGameService,
  searchGameService,
  getApprovedGameListService,
  updateGameStatusService
} from '@/api/game'

import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useGameStore = defineStore(
  'gameList',
  () => {
    const game = ref([])
    const gameBusList = ref([])
    const GameDetail = ref({})
    const getGameDetail = async (id) => {
      const res = await getGameDetailService(id)
      GameDetail.value = res.data
    }

    // 获取已通过审核的游戏列表
    const getApprovedGameList = async () => {
      const res = await getApprovedGameListService()
      game.value = res.data
    }

    const getGamelist = async () => {
      const res = await getGameListService()

      game.value = res.data
    }
    const getGameListBySeller = async (id) => {
      const res = await getGameListBySellerService(id)
      gameBusList.value = res.data
      console.log(gameBusList.value)
    }
    const addGame = async (obj) => {
      const res = await addGameService(obj)

      return res.data
    }
    const setGame = (obj) => {
      game.value = obj
    }
    const searchGame = async (data) => {
      const res = await searchGameService(data)
      console.log(res)
      if (res && res.data) {
        game.value = res.data
      } else {
        console.error('返回结果未包含数据:', res)
      }
    }
    const updateGameStatus = async (data) => {
      await updateGameStatusService(data)
    }
    return {
      game,
      getGamelist,
      getGameDetail,
      setGame,
      gameBusList,
      getGameListBySeller,
      addGame,
      GameDetail,
      searchGame,
      getApprovedGameList,
      updateGameStatus
    }
  },
  {
    persist: true
  }
)
